//
//  SeekTitleCell.m
//  ebook
//
//  Created by mt on 17/1/9.
//  Copyright © 2017年 Moton. All rights reserved.
//

#define kFont 14.0  //字号
#define kTitleWidth [@"热门书籍" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFont]}].width//4个字宽度

#define kNormalTextColor MTColor(100, 100, 100, 1.0)
#define kSelectTextColor MTConfigColor(@"MTNavigationBar_background_Color")

#import "NativeStoreOptionsCell.h"
#import "NativeStoreOptionsModel.h"
#import "UIImage+RTTint.h"

@interface NativeStoreOptionsCell ()

@property (nonatomic, strong) NSArray *optionsArray;
@property (nonatomic, strong) UIButton *packBtn;
@property (nonatomic, strong) NSArray *row0Array;
@property (nonatomic, strong) NSArray *row1Array;
@property (nonatomic, strong) NSArray *row2Array;

@end

@implementation NativeStoreOptionsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier indexOfRow:(NSInteger)row optionsArray:(NSArray *)optionsArr {
    
    _optionsArray = [NSArray arrayWithArray:optionsArr];
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self drawViewWithRow:row optionsArray:optionsArr];
    }
    return self;
}

- (void)drawViewWithRow:(NSInteger)row optionsArray:(NSArray *)optionsArr{
    
    switch (row) {
        case 0:
        {
            NSMutableArray *array = [NSMutableArray array];
            NSArray *titles = @[@"热门书籍", @"最新上架", @"评分最高"];
            for (int i = 0 ; i < titles.count; i++) {
                UIButton *btn = [self btnWithFrame:CGRectMake(15+(kTitleWidth+10)*i, 0, kTitleWidth, 44) title:titles[i] tag:i];
                [btn addTarget:self action:@selector(row0BtnClick:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:btn];
                [array addObject:btn];
            }
            _row0Array = [NSArray arrayWithArray:array];
            UIButton *packBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            packBtn.frame = CGRectMake(MTScreenW-95, 0, 80, 44);
            packBtn.titleLabel.font = [UIFont systemFontOfSize:kFont];
            [packBtn setTitle:@"收起" forState:UIControlStateNormal];
            [packBtn setTitleColor:kNormalTextColor forState:UIControlStateNormal];
            [packBtn setImage:[[UIImage imageNamed:@"cellclose"] rt_tintedImageWithColor:kSelectTextColor] forState:UIControlStateNormal];
            [packBtn setImage:[[UIImage imageNamed:@"cellopen"] rt_tintedImageWithColor:kSelectTextColor] forState:UIControlStateSelected];
            packBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
            packBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 60, 0, 0);
            _packBtn = packBtn;
            
            [packBtn addTarget:self action:@selector(packBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:packBtn];
        }
            break;
            
        case 1:
        {
            NSMutableArray *array = [NSMutableArray array];
            UIButton *allBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            allBtn.frame = CGRectMake(15, 0, kTitleWidth/2, 44);
            [allBtn setTitle:@"全部" forState:UIControlStateNormal];
            [allBtn setTitle:@"全部" forState:UIControlStateSelected];
            [allBtn setTitleColor:kNormalTextColor forState:UIControlStateNormal];
            [allBtn setTitleColor:kSelectTextColor forState:UIControlStateSelected];
            allBtn.titleLabel.font = [UIFont systemFontOfSize:kFont];
            allBtn.tag = 0;
            if (allBtn.tag==0) {
                allBtn.selected = YES;
            }
            [allBtn addTarget:self action:@selector(row1BtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:allBtn];
            [array addObject:allBtn];
            
            for (int i = 0; i < optionsArr.count; i++) {
                NativeStoreOptionsModel *model = optionsArr[i];
                UIButton *btn = [self btnWithFrame:CGRectMake(CGRectGetMaxX(allBtn.frame)+15+(kTitleWidth+10)*(i%4), 30*((int)i/4), kTitleWidth, 44) title:model.name tag:i+1];
                [btn addTarget:self action:@selector(row1BtnClick:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:btn];
                [array addObject:btn];
            }
            [_packBtn setTitle:@"收起" forState:UIControlStateNormal];
            _row1Array = [NSArray arrayWithArray:array];
        }
            break;
        case 2:
        {
            NSMutableArray *array = [NSMutableArray array];
            NSArray *titles = @[@"全部", @"连载", @"完结"];
            for (int i = 0 ; i < titles.count; i++) {
                UIButton *btn = [self btnWithFrame:CGRectMake(15+(kTitleWidth/2+20)*i, 0, kTitleWidth/2, 44) title:titles[i] tag:i];
                [btn addTarget:self action:@selector(row2BtnClick:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:btn];
                [array addObject:btn];
            }
            _row2Array = [NSArray arrayWithArray:array];
        }
            break;
        default:
            break;
    }
}

- (UIButton *)btnWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag {
  
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateSelected];
    [btn setTitleColor:kNormalTextColor forState:UIControlStateNormal];
    [btn setTitleColor:kSelectTextColor forState:UIControlStateSelected];
    btn.titleLabel.font = [UIFont systemFontOfSize:kFont];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn.tag = tag;
    if (btn.tag==0) {
        btn.selected = YES;
    }
    return btn;
}

//热门书籍 ... 点击
- (void)row0BtnClick:(UIButton *)btn {
    
    for (UIButton *btn0 in _row0Array) {
        btn0.selected = NO;
    }
    btn.selected = YES;
    if ([self.delegate respondsToSelector:@selector(nativeStoreOptionsCell:hotSelectAtIndex:)]) {
        [self.delegate nativeStoreOptionsCell:self hotSelectAtIndex:btn.tag-1];
    }
}

//收起 点击
- (void)packBtnClick:(UIButton *)btn {
    
    if ([btn.titleLabel.text hasPrefix:@"收起"]) {
        [btn setTitle:@"展开" forState:UIControlStateNormal];
        btn.selected = YES;
    } else {
        [btn setTitle:@"收起" forState:UIControlStateNormal];
        btn.selected = NO;
    }

    if ([self.delegate respondsToSelector:@selector(nativeStoreOptionsCell:cellIsClosed:)]) {
        [self.delegate nativeStoreOptionsCell:self cellIsClosed:btn.selected];
    }
}

- (void)row1BtnClick:(UIButton *)btn {
    
    for (UIButton *btn1 in _row1Array) {
        btn1.selected = NO;
    }
    btn.selected = YES;
    if ([self.delegate respondsToSelector:@selector(nativeStoreOptionsCell:selectWithModel:)]) {
        [self.delegate nativeStoreOptionsCell:self selectWithModel:btn.tag==0?nil:_optionsArray[btn.tag-1]];
    }
}

//全部，连载，完结  点击
- (void)row2BtnClick:(UIButton *)btn {
    
    for (UIButton *btn2 in _row2Array) {
        btn2.selected = NO;
    }
    btn.selected = YES;
    if ([self.delegate respondsToSelector:@selector(nativeStoreOptionsCell:fullSelectAtIndex:)]) {
        [self.delegate nativeStoreOptionsCell:self fullSelectAtIndex:btn.tag-1];
    }
}

@end
